package djboom.materials.display.elements {
	import djboom.materials.events.TableDataCellEvent;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaSprite;

	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 18.05.2010
	 * 
	 */

	public class TableData extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------
	
		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _tableBitmap:CasaBitmap;
		private var _tableCells:CasaSprite;
		private var _cellName:String;
		
		private var _scrollHeight:int;
		
		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function TableData(bitmapData:BitmapData, dataXML:XML, tableCellsPoint:Point, cellTextFormat:TextFormat) {
			super();
			
			_tableBitmap = new CasaBitmap(bitmapData);
			addChild(_tableBitmap);
			
			_tableCells = new CasaSprite();
			_tableCells.x = tableCellsPoint.x;			_tableCells.y = tableCellsPoint.y;
			addChild(_tableCells);
			
			var rowList:XMLList = dataXML.child("row");
			
			for each (var rowXML:XML in rowList) {
				var cellList:XMLList = rowXML.child("cell");
				
				for each (var cellXML:XML in cellList) {
					var cellAlign:String = cellXML.attribute("align").toString();
					var cellWidth:int = int(cellXML.attribute("width").toString());
					var cellData:String = cellXML.hasOwnProperty("data") ? cellXML.child("data").toString() : null;					var checkData:String = cellXML.hasOwnProperty("checkData") ? cellXML.child("checkData").toString() : null;
					
					var tableDataCell:TableDataCell = new TableDataCell(cellAlign, cellWidth, cellData, checkData, cellTextFormat);
					tableDataCell.name = String(rowXML.childIndex()) + String(cellXML.childIndex());
					tableDataCell.y = rowXML.childIndex() * 37;
					tableDataCell.addEventListener(TableDataCellEvent.INPUT_ERROR, inputError);					tableDataCell.addEventListener(TableDataCellEvent.INPUT_SUCCESS, inputSuccess);					tableDataCell.addEventListener(TableDataCellEvent.INPUT_LAST_CELL_ROW, inputLastCellRow);					tableDataCell.addEventListener(TableDataCellEvent.INPUT_LAST_CELL, inputLastCell);					
					if (cellXML.childIndex() + 1 == cellList.length()) {
						tableDataCell.lastCellRow = true;
						if (rowXML.childIndex() + 1 == rowList.length()) {
							tableDataCell.lastCell = true;    
						}	
					}
					
					try {
						var previousCell:TableDataCell = _tableCells.getChildByName(String(rowXML.childIndex()) + String(cellXML.childIndex() - 1)) as TableDataCell;
						
						tableDataCell.x = previousCell.x + previousCell.cellWidth + 2;
					} catch(error:Error) {
					}
					
					_tableCells.addChild(tableDataCell);
				}
			}
			
			_scrollHeight = this.height;
			
			addEventListener(Event.REMOVED_FROM_STAGE, clearContent);
		}	 

		private function inputLastCellRow(event:TableDataCellEvent):void {
			dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_LAST_CELL_ROW));
		}

		private function inputLastCell(event:TableDataCellEvent):void {
			dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_LAST_CELL));
		}

		private function inputError(event:TableDataCellEvent):void {
			dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_ERROR));			
		}

		private function inputSuccess(event:TableDataCellEvent):void {
			dispatchEvent(new TableDataCellEvent(TableDataCellEvent.INPUT_SUCCESS));
		}

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clearContent(event:Event):void {
			if (_tableCells != null) {
				while (_tableCells.numChildren) {
					var tableDataCell:TableDataCell = _tableCells.getChildAt(0) as TableDataCell;
                
					tableDataCell.destroy();
					tableDataCell = null;
				}
            
				_tableCells.destroy();
				_tableCells = null;
			}
			
			_tableBitmap.destroy();
			_tableBitmap = null;
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function enableCell(cellName:String):void {
			_cellName = cellName;
			
			var cell:TableDataCell = _tableCells.getChildByName(_cellName) as TableDataCell;
			cell.enableInput();
		}

		public function repeatEnableCell():void {
			var cell:TableDataCell = _tableCells.getChildByName(_cellName) as TableDataCell;
			cell.enableInput();
		}
		
		public function get scrollHeight():int {
			return _scrollHeight;
		}
		
			// ----------------------------------------------------------------------------
	        // Публичные свойства.
	        // ----------------------------------------------------------------------------
	}
}
